package com.ljx.recruitment.sale.controller;

import com.ljx.recruitment.common.domain.vo.resp.ApiResult;
import com.ljx.recruitment.sale.domain.vo.req.AccessTokenSubmitReq;
import com.ljx.recruitment.sale.service.AccessTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api(tags = "匿名访问Token表")
@Controller
@RequestMapping("/access_token")
public class AccessTokenController {

    @Autowired
    private AccessTokenService accessTokenService;

    @ApiOperation("生成访问Token")
    @ResponseBody
    @PostMapping("/submit")
    public ApiResult<String> submitAccessToken(@RequestBody AccessTokenSubmitReq req){
        return ApiResult.success(accessTokenService.submit(req));
    }

    @ApiOperation("解析并跳转")
    @GetMapping("/parse/{token}")
    public void parseToken(@PathVariable("token") String token,
                             HttpServletResponse response){
        String redirectUrl = accessTokenService.parseToken(token);
        try {
            response.sendRedirect(redirectUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
